<?php
declare (strict_types = 1);

namespace app\command;

use EasyWeChat\Factory;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

class Js extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('js')
            ->setDescription('the js command');        
    }
    //自动解散
    protected function execute(Input $input, Output $output)
    {
        $OrderFleet_model = app('app\admin\model\OrderFleet');
        $OrderMember_model = app('app\admin\model\OrderMember');
        //解散时间到了还没满员的车队
        $data = $OrderFleet_model->where([
            ['status','=',1],
            ['arrival_day','<=',date('m月d日')],
            ['arrival_time','<',date('H:i')],
        ])->select();
        if(!empty($data) && !empty($data->toArray())){
            $res = $data->toArray();
            $data->update([
                'status'=>5
            ]);
            $script_data = app('app\admin\model\Script')->where('id',$res['script_id'])->find()->toArray();
            $ids = [];
            foreach ($res as $v){
                $ids[] = $v['id'];
            }
            $ids = implode(',',$ids);
            $member_data = $OrderMember_model->where([['order_fleet_id','in',$ids],['status','=',1]])->with(['user_index'])->select();
            $res_data = $member_data->toArray();
            $member_data->update(['status'=>2]);
            $Msg_model = app('app\api\model\Msg');
            $arr = [];
            $app = Factory::miniProgram([
                'app_id' => 'wx2689638cda0e034e',
                'secret' => '44f9ba8ce2eb2f49ba17feee2915a0ae',
            ]);
            foreach ($res_data as $v){
                //批量组装站内消息
                $arr[] = [
                    'type'=>1,
                    'name'=>'您所在的车队已解散',
                    'content'=>'您所在的关于"'.$script_data.'"的车队已解散',
                    'no_content'=>'车队队长解散'
                ];
                //发送微信订阅消息-活动取消通知
                if(!empty($v['user_index']['openid'])){
                    $dy_data = [
                        'template_id' => '1do50m-mPDOQ9Y1__7VULQwb48ttNo1lQWtU_8d0YZQ', // 所需下发的订阅模板id
                        'touser' => $v['user_index']['openid'],     // 接收者（用户）的 openid
                        'page' => '',       // 点击模板卡片后的跳转页面，仅限本小程序内的页面。支持带参数,（示例index?foo=bar）。该字段不填则模板无跳转。
                        'data' => [         // 模板内容，格式形如 { "key1": { "value": any }, "key2": { "value": any } }
                            'thing1' => [
                                'value' => $script_data['name'],
                            ],
                            'time2' => [
                                'value' => date('Y-m-d H:i:s'),
                            ],
                            'thing3' => [
                                'value' => '活动开始时间已到，人数依然不齐，自动解散',
                            ],
                        ],
                    ];
                    $app->subscribe_message->send($dy_data);
                }
            }
            //批量添加组装好的站内消息
            $Msg_model->saveAll($arr);
        }
    	$output->writeln('js队伍');
    }
}
